/**
 *
 */
angular
    .module('baocloud.createCloud')
    .factory('uuid', uuid)
    .directive('selectRect', selectRect)
    .factory('selectRectHandle', selectRectHandle)
    .controller('SelectRectController', SelectRectController)
    .controller('CreateCloudController', CreateCloudController);


function selectRect() {
    var directive = {
        restrict: 'AE',
        transclude: true,
        scope: {
            id: '@',
            options: '=',
            selectAll: '='
        },
        templateUrl: function (element, attrs) {
            return attrs.templateUrl || 'CreateCloud/selectRect.html';
        },
        controller: 'SelectRectController',
        controllerAs: 'rect'

    };
    return directive;
}


function ubtnGroupHandle() {
    return {
        onTabClick: function () {}
    }
}

function selectRectHandle() {
    return {
        remove: function () {}
    }
}

function uuid() {
    return {
        create: function () {
            var len = 32; //32长度
            var radix = 16; //16进制
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [],
                i;
            radix = radix || chars.length;
            if (len) {
                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
            } else {
                var r;
                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';
                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random() * 16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }
            return uuid.join('');
        }
    }
}

function SelectRectController($scope, selectRectHandle) {
    var self = this;
        var index = 0;
        var id = self.id = $scope.id;
        var options = self.options = $scope.options;
        var selectAll = $scope.selectAll;

        self.selected= options[index].code;

        self.onChange = function() {
            _.each(selectAll,function (item,nIdx) {
                if(item._id === id){
                    item.label = item.code = self.selected;
                }
            });

        };

        self.close = function() {
            var testa = _.reject(selectAll, function(item){
                return item._id === id;
            });
            var idx = 0;
            for(idx;idx<selectAll.length;idx++){
                if(selectAll[idx]._id === id){
                    break;
                }
            }
            selectAll.splice(idx,1);
            //console.log('SelectRectController-------->%s,%s',id,idx);
        }

}

function CreateCloudController($scope, selectRectHandle, uuid, ISP) {
    var self = this;
    self.model = {};

    var bandwidthDatasets = [{
        label: '上海',
        areas: [{
            label: '罗泾'
        }]
    }, {
        label: '重庆',
        areas: [{
            label: '重庆'
        }]
    }];

    self.uhostLocals = {
        datasets: bandwidthDatasets,
        selected: {
            local: '0',
            area: '0'
        },
        tabChange: function (code) {
            this.selected.local = code;
        },
        diff: function () {
            return {
                local: this.datasets[this.selected.local].label,
                area: this.datasets[this.selected.local].areas[this.selected.area].label,
            }
        }
    };

    var networkDataSource = [{
        "subnetId": "22172",
        "vpcNetAddress": "10.100.0.0/16",
        "subnetNetAddress": "10.100.0.0/24",
        "vpcCiId": "20653"
    }, {
        "subnetId": "22173",
        "vpcNetAddress": "10.100.0.0/16",
        "subnetNetAddress": "10.100.255.0/24",
        "vpcCiId": "20653"
    }, {
        "subnetId": "20654",
        "vpcNetAddress": "10.100.0.0/16",
        "subnetNetAddress": "10.100.1.0/24",
        "vpcCiId": "20653"
    }, {
        "subnetId": "22169",
        "vpcNetAddress": "10.100.0.0/16",
        "subnetNetAddress": "10.100.253.0/24",
        "vpcCiId": "20653"
    }, {
        "subnetId": "22168",
        "vpcNetAddress": "10.100.0.0/16",
        "subnetNetAddress": "10.100.254.0/24",
        "vpcCiId": "20653"
    }, {
        "subnetId": "21357",
        "vpcNetAddress": "10.100.0.0/16",
        "subnetNetAddress": "10.100.4.0/24",
        "vpcCiId": "20653"
    }, {
        "subnetId": "213560",
        "vpcNetAddress": "10.100.1.0/16",
        "subnetNetAddress": "10.100.3.0/24",
        "vpcCiId": "20652"
    }, {
        "subnetId": "213550",
        "vpcNetAddress": "10.100.0.1/16",
        "subnetNetAddress": "10.100.2.0/24",
        "vpcCiId": "20652"
    }];

    var networkDataSource = [];
    var networkDatasets = [];

    if(networkDataSource.length === 0){
        networkDatasets.push({
                vpcCiId: 0,
                vpcNetAddress: '请选择VPC网络',
                subnets: []
            });
    }
    _.chain(networkDataSource)
        .pluck('vpcCiId')
        .uniq()
        .map(function (item) {
            var subnets = _.where(networkDataSource, {
                vpcCiId: item
            });
            var vpcNetAddress = _.uniq(_.pluck(_.where(networkDataSource, {
                vpcCiId: item
            }), 'vpcNetAddress'));
            vpcNetAddress = vpcNetAddress.length >= 0 ? vpcNetAddress[0] : '';
            networkDatasets.push({
                vpcCiId: item,
                vpcNetAddress: vpcNetAddress,
                subnets: subnets
            });
        });
    console.log('--->', networkDatasets);

    self.uhostNetwork = {
        datasets: networkDatasets,
        tabsModel: [{
            label: '暂不需要'
        }, {
            label: '需要'
        }],
        selected: {
            vpcnet: '0',
            subnet: '0',
            internet: '0'
        },
        tabChange: function (code) {
            this.selected.internet = code;
        },
        diff: function () {
            return {
                vpcCiId: this.datasets[this.selected.vpcnet].vpcCiId,
                vpcNetAddress: this.datasets[this.selected.vpcnet].vpcNetAddress,
                subnetId: this.datasets[this.selected.vpcnet].subnets[this.selected.subnet].subnetId,
                subnetNetAddress: this.datasets[this.selected.vpcnet].subnets[this.selected.subnet].subnetNetAddress,
                internet: this.selected.internet
            }
        }
    }

    var bandwidthDataSource = [{
        "ciCode": "PBWTDC0001",
        "customerId": "14",
        "ciName": "SSLVPN因特网移动线路带宽(PBWTDC0001)",
        "netOperator": "移动",
        "ciId": "13932",
        "bandwidthType": "独占带宽"
    }, {
        "ciCode": "PBWTDC0002",
        "customerId": "14",
        "ciName": "SSLVPN因特网联通线路带宽(PBWTDC0002)",
        "netOperator": "联通",
        "ciId": "13933",
        "bandwidthType": "独占带宽"
    }, {
        "ciCode": "PBWTDC0003",
        "customerId": "14",
        "ciName": "SSLVPN因特网电信线路带宽(PBWTDC0003)",
        "netOperator": "电信",
        "ciId": "13934",
        "bandwidthType": "独占带宽"
    }, {
        "ciCode": "PBWT320001",
        "customerId": "14",
        "ciName": "因特网网络带宽(PBWT320001)",
        "netOperator": "联通",
        "ciId": "20670",
        "bandwidthType": "独占带宽"
    }, {
        "ciCode": "PBWT320002",
        "customerId": "14",
        "ciName": "因特网网络带宽(PBWT320002)",
        "netOperator": "联通",
        "ciId": "22073",
        "bandwidthType": "独占带宽"
    }];

    var bandwidthDatasets = [{
        label: '独占带宽',
        netOperators: []
    }, {
        label: '共享带宽',
        netOperators: [{
            label: '联通',
            bandwidths: []
        }, {
            label: '电信',
            bandwidths: []
        }, {
            label: '移动',
            bandwidths: []
        }]
    }];

    // var bandwidthDatasets = [{
    //     label: '独占带宽',
    //     code: '01',
    //     netOperators: [{
    //         label: '联通',
    //         code: '01',
    //         bandwidths: [{
    //             ciCode: 'PBWT320001',
    //             ciName: "因特网网络带宽(PBWT320001)"
    //         }, {
    //             ciCode: 'PBWT320002',
    //             ciName: "因特网网络带宽(PBWT320002)"
    //         }]
    //     }, {
    //         label: '电信',
    //         code: '02',
    //         bandwidths: [{
    //             ciCode: 'PBWT320011',
    //             ciName: "因特网网络带宽(PBWT320011)"
    //         }, {
    //             ciCode: 'PBWT320012',
    //             ciName: "因特网网络带宽(PBWT320012)"
    //         }]
    //     }, {
    //         label: '移动',
    //         code: '03',
    //         bandwidths: [{
    //             ciCode: 'PBWT320021',
    //             ciName: "因特网网络带宽(PBWT320021)"
    //         }, {
    //             ciCode: 'PBWT320022',
    //             ciName: "因特网网络带宽(PBWT320022)"
    //         }]
    //     }]
    // }, {
    //     label: '共享带宽',
    //     code: '02',
    //     netOperators: [{
    //         label: '联通12',
    //         code: '01',
    //         bandwidths: []
    //     }, {
    //         label: '电信',
    //         code: '02',
    //         bandwidths: []
    //     }, {
    //         label: '移动',
    //         code: '03',
    //         bandwidths: []
    //     }]
    // }];

    _.chain(bandwidthDataSource)
        .pluck('netOperator')
        .uniq()
        .map(function (item) {
            var bandwidths = _.where(bandwidthDataSource, {
                netOperator: item
            });
            bandwidthDatasets[0].netOperators.push({
                label: item,
                bandwidths: bandwidths
            });
        });

    self.uhostBandwidth = {
        datasets: bandwidthDatasets,
        selected: {
            tab: '0',
            ISP: '0',
            type: '0'
        },
        tabChange: function (code) {
            this.selected.tab = code;
        },
        diff: function () {
            return {
                bandwidthType: this.datasets[this.selected.tab].label,
                netOperator: this.datasets[this.selected.tab].netOperators[this.selected.ISP].label,
                bandwidth: this.datasets[this.selected.tab].netOperators[this.selected.ISP].bandwidths[this.selected.type] || {}
            }
        }
    }
    var imageDataSource = [{
        "name": "RHEL6.7 64位",
        "cname": "RHEL6.7 64位"
    }, {
        "name": "Windows2008 R2 64位",
        "cname": "Windows2008 R2 64位"
    }, {
        "name": "CentOS6.7 64位",
        "cname": "CentOS6.7 64位"
    }, {
        "name": "RHEL6.7 64位",
        "cname": "RHEL6.7 64位"
    }, {
        "name": "Windows2008 R2 64位",
        "cname": "Windows2008 R2 64位"
    }]

    self.uhostISO = {
        datasets: imageDataSource,
        selected: '',
        diff: function () {
            return {
                image: this.selected || ''
            }
        }
    }

    //************存储定义
    self.uhostStorage = {
        unit: 'GB',
        sysDisk : '50',
        selected:[],
        options:[{label:'50',code:'50'},{label:'100',code:'100'},{label:'200',code:'200'},{label:'300',code:'300'},{label:'400',code:'400'},{label:'500',code:'500'}],
        storageAdd: function () {
            this.selected.push({
                label: '50',
                code: '50',
                _id: uuid.create()
            });
            console.log('aaaa');
        },
        diff: function(){
            return {
                unit: this.unit,
                sysDisk: this.sysDisk,
                dataDisk: _.pluck(this.selected,'code')
            }
        }
    };
    ////////////////////////////////////////////
    self.submit = function () {
        console.log(JSON.stringify(self.uhostBandwidth.diff()));
        console.log(JSON.stringify(self.uhostISO.diff()));
        console.log(JSON.stringify(self.uhostLocals.diff()));
        console.log(JSON.stringify(self.uhostNetwork.diff()));
        console.log(JSON.stringify(self.uhostStorage.diff()));
        
    }


}